<template>
  <div class="app-container">
    <div class="search-container" style="display: flex">
      <el-form-item label="搜索" style="width: 200px; margin-left: 10px">
        <el-input v-model="params.search" placeholder="请输入关键词" />
      </el-form-item>
      <el-button type="primary" style="margin-left: 10px" @click="clearParams"
        >清空</el-button
      >
      <el-button type="primary" style="margin-left: 10px" @click="handleSearch"
        >查询</el-button
      >
      <el-button
        type="success"
        style="margin-left: 10px"
        @click="handleAddSupervisor"
        >添加监督员</el-button
      >
    </div>

    <el-card shadow="never" class="table-container">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="superId" label="编号" />
        <el-table-column prop="sname" label="用户名" />
        <el-table-column prop="sphone" label="电话" />
        <el-table-column label="性别">
          <template #default="scope">
            {{ scope.row.ssex == 0 ? "女" : "男" }}
          </template>
        </el-table-column>
        <el-table-column sortable prop="createTime" label="生日">
          <template #default="scope">
            {{ formatDateTime(new Date(scope.row.sbirth)) }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button
              link
              type="primary"
              size="small"
              @click="handleUpdate(scope.row)"
            >
              更新
            </el-button>
            <el-button
              link
              type="primary"
              size="small"
              @click="handleDelete(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="margin-top: 20px"
        background
        layout="total, prev, pager, next, jumper"
        :total="pageRes.total"
        :page-size="params.pageSize"
        @current-change="pageChange"
      />
    </el-card>
    <el-dialog v-model="updateDialogFormVisible" title="更新" width="800">
      <el-form label-width="auto" style="max-width: 400px; margin-top: 20px">
        <el-form-item label="ID">
          <el-input v-model="curRow.superId" disabled />
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="curRow.sname" disabled />
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="curRow.sphone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="性别" placeholder="请选择性别">
          <el-select v-model="curRow.ssex">
            <el-option label="男" :value="1" />
            <el-option label="女" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="生日">
          <el-date-picker
            v-model="curRow.sbirth"
            type="date"
            placeholder="请选择生日"
          />
        </el-form-item>
        <el-form-item label="密码">
          <el-input
            type="password"
            v-model="curRow.spassword"
            placeholder="输入即更新密码，不输入不更新密码"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="updateDialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmUpdate"> 确定 </el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog v-model="addDialogFormVisible" title="添加监督员" width="800">
      <el-form>
        <el-form-item label="用户名">
          <el-input v-model="addForm.sname" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="addForm.sphone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="addForm.ssex" placeholder="请选择性别">
            <el-option label="男" :value="1" />
            <el-option label="女" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="生日">
          <el-date-picker
            v-model="addForm.sbirth"
            type="date"
            placeholder="请选择生日"
          />
        </el-form-item>
        <el-form-item label="密码">
          <el-input
            type="password"
            v-model="addForm.spassword"
            placeholder="请输入密码"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="addDialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmAdd"> 确定 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import SupervisorAPI from "@/api/supervisor";
import DistrictAPI from "@/api/district";
import RoleAPI from "@/api/role";
import { ElMessage, ElMessageBox } from "element-plus";
import { fa } from "element-plus/es/locale";

const params: any = ref({
  searchField: "s_name",
  search: "",
  pageSize: 5,
  curPage: 1,
});

const updateDialogFormVisible = ref(false);
const addDialogFormVisible = ref(false);

const curRow: any = ref({});

const updateParams: any = ref({
  id: "",
  email: "",
  nickName: "",
  note: "",
});

const pageRes = ref({
  totalPage: 0,
  total: 0,
});

const tableData: any = ref([]);

const roleList: any = ref([]);

const addForm: any = reactive({
  sphone: "",
  sname: "",
  ssex: "",
  sbirth: "",
  spassword: "",
});

function reqSupervisorList() {
  SupervisorAPI.getSupervisorList(params.value).then((res) => {
    console.log(res);
    tableData.value = [];
    pageRes.value.totalPage = res.pages;
    pageRes.value.total = res.total;
    for (const v of res.records) {
      v.spassword = "";
      tableData.value.push(v);
    }
  });
}

function pageChange(v: any) {
  console.log(v);
  console.log();
  params.value.curPage = v;
  reqSupervisorList();
}

function pad(num: any) {
  return num.toString().padStart(2, "0");
}

function formatDateTime(date: any) {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  return `${year}-${pad(month)}-${pad(day)} ${pad(hour)}:${pad(minute)}:${pad(second)}`;
}

function verify(row: any) {
  curRow.value = Object.assign({}, row);
  console.log(curRow.value);
  updateDialogFormVisible.value = true;
}

function handleUpdate(row: any) {
  curRow.value = Object.assign({}, row);
  console.log(curRow.value);
  updateDialogFormVisible.value = true;
}

function handleDelete(row: any) {
  ElMessageBox.confirm("确认删除?", "", {
    confirmButtonText: "OK",
    cancelButtonText: "取消",
    type: "确认",
  }).then(() => {
    SupervisorAPI.deleteSupervisor(row.superId).then((res) => {
      ElMessage({
        type: "success",
        message: "删除成功",
      });
      reqSupervisorList();
    });
  });
}

onMounted(() => {
  reqSupervisorList();
});

function clearParams() {
  params.value = {
    searchField: "s_name",
    search: "",
    pageSize: 5,
    curPage: 1,
  };
}

function handleSearch() {
  reqSupervisorList();
}

function confirmUpdate() {
  curRow.value.supervisorId = curRow.value.superId;
  SupervisorAPI.updateSupervisor(curRow.value).then((res) => {
    ElMessage({
      showClose: true,
      message: "更新成功",
      type: "success",
    });
    updateDialogFormVisible.value = false;
    reqSupervisorList();
  });
}

function reqRoleList() {
  roleList.value = [];
  RoleAPI.getRoleList(null).then((res) => {
    console.log(res);
    for (const v of res.list) {
      roleList.value.push(v);
    }
  });
}

function handleAddSupervisor() {
  addForm.sphone = "";
  addForm.sname = "";
  addForm.ssex = "";
  addForm.sbirth = "";
  addForm.spassword = "";
  addDialogFormVisible.value = true;
}

function confirmAdd() {
  console.log(addForm);
  SupervisorAPI.addSupervisor(addForm).then((res) => {
    console.log(res);
    ElMessage({
      showClose: true,
      message: "添加成功",
      type: "success",
    });
    addDialogFormVisible.value = false;
    reqSupervisorList();
    addForm.sname = "";
    addForm.sphone = "";
    addForm.ssex = "";
    addForm.sbirth = "";
    addForm.spassword = "";
  });
}
</script>
